package com.itheima.Array.sort;

import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: Ethan Yankang
 * @Program: code-random-recordings
 * @Date: 2024-08-06 13:01
 **/
    public class Main {

    //记录每一个排序耗时
    static List<Integer> timeConsuming=new LinkedList<>();
    //将要排序的算法提前录入
    static String[] sortNames={"选择排序","冒泡排序","插入排序","快速排序","希尔排序","归并排序","计数排序","桶排序","堆排序","基数排序"};

    public static void main(String[] args) {

        int size = 50000 - 1;  // 数组大小
        int[] nums = new int[size];

        // 生成从1到100000的数字数组
        for (int i = 0; i < size; i++) {
            nums[i] = size - i;
        }
        // 运行各大排序算法
        for (int i = 0; i <10 ; i++) {
            test(sortNames[i],nums);
        }

        System.out.println();
        System.out.println();
        System.out.println("===========================================================================================");
        System.out.println("耗时分别如下：");
        for (int i = 0; i < 10; i++) {
            System.out.println(sortNames[i]+"耗时"+timeConsuming.get(i).intValue()+"ms");
        }
    }

        /*
         * @description 正式跑算法+计时
         * @params
         * @return
         */

    private static void test(String SortName,int[] nums){

        System.out.println(SortName);
        System.out.println("Before sort,nums[]：");
        System.out.println(Arrays.toString(nums));
        System.out.println("After sort,nums[]: ");
        int[] nums10= nums.clone();
        //  计时器
        Instant start = Instant.now();
        chooseSort(SortName,nums10);
        Instant end = Instant.now();
        Duration time10 = Duration.between(start, end);

        System.out.println(Arrays.toString(nums10));
        System.out.println(SortName+"time=" + time10.toMillis());
        timeConsuming.add((int) time10.toMillis());
    }

/*
 * @description 具体选择哪一种算法
 * @params
 * @return
 */

    private static void chooseSort(String sortName,int[] nums) {

        int index=0;
        while (!sortNames[index].equals(sortName)) {
            index++;
        }
        System.out.println("index = " + index);
            switch (index){
            case 0:
                SelectSort.selectSort(nums);
            case 1:
                BubbleSort.bubblesort(nums);
            case 2:
                InsertSort.insertSort(nums);
            case 3:
                QuickSort.quickSort(nums,0, nums.length-1);
            case 4:
                ShellSort.shellSort(nums);
            case 5:
                MergeSort.mergeSort(nums,0, nums.length-1);
            case 6:
                CountingSort.countingSort(nums);
            case 7:
                BucketSort.bucketSort(nums);
            case 8:
                HeapSort.heapSort(nums);
            case 9:
                RadixSort.radixSort(nums);
        }
    }
}


